package com.factory.iamp.utils.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Aspect
@Component
public class timeAop {
    private final static String POINT = "execution(* com.factory.iamp.service.*.*(..))";
    private static final Logger logger = LoggerFactory.getLogger(timeAop.class);

    @Pointcut(POINT)
    public void recordLog() {}

    @Around("recordLog()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
        logger.info("----------开始执行 " + methodName + "----------");
        stopWatch.start();
        Object obj = pjp.proceed(pjp.getArgs());
        stopWatch.stop();
        long cost = stopWatch.getTotalTimeMillis();
        logger.info("----------执行 " + methodName + " 结束 ，用时 " + cost + " ms----------");
        return obj;
    }
}
